package com.haipumi.biz.impl;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;

import com.haipumi.beans.AccountOperation;
import com.haipumi.beans.haipumi_user;
import com.haipumi.biz.OpMailSender;

@Service("opMialSenderImpl")
public class OpMialSenderImpl implements OpMailSender{
	private JavaMailSender mailSender;
	private VelocityEngine velocityEngine;
	
	/**
	 * @param mailSender the mailSender to set
	 */
	@Resource(name="mailSender")
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	/**
	 * @param velocityEngine the velocityEngine to set
	 */
	@Resource(name="velocityEngine")
	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

	public void sendOpMail(final AccountOperation accountOperation) {
		switch(accountOperation.getOptype()){
        	case deposite:
        	case withdraw:
        		 MimeMessagePreparator preparator = new MimeMessagePreparator() {
     	            public void prepare(MimeMessage mimeMessage) throws Exception {
     	                MimeMessageHelper message = new MimeMessageHelper(mimeMessage,"utf-8");
     	                message.setSubject("银行操作");
     	                message.setTo(accountOperation.getEmail());
     	                message.setFrom("15886428332@163.com"); // could be parameterized...
     	                Map model = new HashMap();
     	                model.put("accountOperation", accountOperation);
     	                String text = VelocityEngineUtils.mergeTemplateIntoString(
     	 	                        velocityEngine, "com/yc/web/view/vm/depositewithdraw.vm","utf-8",model);
     	                message.setText(text, true);
     	            }
     	        };
     	        this.mailSender.send(preparator);
        		break;
        	case transfer:
        		MimeMessagePreparator preparator1 = new MimeMessagePreparator() {
      	            public void prepare(MimeMessage mimeMessage) throws Exception {
      	                MimeMessageHelper message = new MimeMessageHelper(mimeMessage,"utf-8");
      	                message.setSubject("银行操作");
      	                message.setTo(accountOperation.getEmail());
      	                message.setFrom("15886428332@163.com"); // could be parameterized...
      	                Map model = new HashMap();
      	                model.put("accountOperation", accountOperation);
      	                String text = VelocityEngineUtils.mergeTemplateIntoString(
      	 	                        velocityEngine, "com/yc/web/view/vm/transferout.vm","utf-8",model);
      	                message.setText(text, true);
      	            }
      	        };
      	        this.mailSender.send(preparator1);
      	        
      	      MimeMessagePreparator preparator2 = new MimeMessagePreparator() {
    	            public void prepare(MimeMessage mimeMessage) throws Exception {
    	                MimeMessageHelper message = new MimeMessageHelper(mimeMessage,"utf-8");
    	                message.setSubject("银行操作");
    	                message.setTo(accountOperation.getInaccountemail());
    	                message.setFrom("15886428332@163.com"); // could be parameterized...
    	                Map model = new HashMap();
    	                model.put("accountOperation", accountOperation);
    	                String text = VelocityEngineUtils.mergeTemplateIntoString(
    	 	                        velocityEngine, "com/yc/web/view/vm/transferin.vm","utf-8",model);
    	                message.setText(text, true);
    	            }
    	        };
    	        this.mailSender.send(preparator2);
        		break;
        }
		  
	}

	public void sendTodayRecordEmail(File file) {
		try {
			MimeMessage message=mailSender.createMimeMessage();
			MimeMessageHelper helper=new MimeMessageHelper(message,true);
			helper.setFrom("15886428332@163.com");
			helper.setTo("1334015786@qq.com");
			helper.setSubject("银行操作记录表");
			helper.setText("记录表：");
			FileSystemResource f=new FileSystemResource(file);
			helper.addAttachment(f.getFilename(), f);
			
			mailSender.send(message);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}

}
